﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.View.Proxy;

namespace EFong.View.Documentation
{
    public partial class Frm_InsurancePolicyManager : BasicMdiChildForm
    {
        private Proxy.MasterServiceProxy _masterProxy {get{ return Proxy.MasterServiceProxy.GetInstance();}}

        public Frm_InsurancePolicyManager()
        {
            InitializeComponent();
        }

        protected override void InitFields()
        {
        }

        protected override void Events()
        {
            this.gv_Orders.FocusedRowChanged += (s, e) =>
                {
                    SelInsp = this.gv_Orders.GetRow(e.FocusedRowHandle) as Contract.InsurancePolicy;
                };
        }

        protected override void LoadDatas()
        {
            this.bds_insurancePolicy.BindingData(_masterProxy.GetAllInsurancePolicy().ReturnValue);
            SetAuthorities();
        }

        protected void SetAuthorities()
        {
            foreach (Contract.RoleAuthorizedItem roleAuthorizedItem in AppParameters.LoginUser.Role.Authorities)
            {
                switch (roleAuthorizedItem.AuthorizedItem.Name)
                {
                    case "保险单管理":
                        bbt_Add.Enabled = roleAuthorizedItem.Allow;
                        bbt_Delete.Enabled = roleAuthorizedItem.Allow;
                        bbt_Reuse.Enabled = roleAuthorizedItem.Allow;
                        bbt_Send.Enabled = roleAuthorizedItem.Allow;
                        break;
                }
            }
        }

        private void bar_Main_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "bbt_Add":
                    this.add();
                    break;
                case "bbt_Delete":
                    this.delete();
                    break;
                case "bbt_Send":
                    this.send();
                    break;
                case "bbt_Close":
                    this.Close();
                    break;
                case "bbt_Reuse":
                    this.reuse();
                    break;
                case "bbt_不可用":
                    this.不可用();
                    break;
                default:
                    break;
            }
        }

        private void add()
        {
            Frm_NewInsurancePolicy frm = new Frm_NewInsurancePolicy();
            frm.Text = "增加保险单";

            if (FormOpener.ShowDialog(frm) == DialogResult.OK)
                LoadDatas();
        }

        private void delete()
        {
            UIOperManager.SaveData(
                () =>
                {
                    SelInsp.Status = "作废";
                    return _masterProxy.UpdateInsurancePolicy(SelInsp).IsSuccess;

                },
                "已成功作废.",
                () =>
                {
                    LoadDatas();
                }
                );
        }

        private void 不可用()
        {
            UIOperManager.SaveData(
                () =>
                {
                    if (SelInsp.Status != "可用")
                    {
                        MsgBox.Error("只有可用的单据才可以不可用");
                        return false;
                    }

                    SelInsp.Status = "不可用";
                    return _masterProxy.UpdateInsurancePolicy(SelInsp).IsSuccess;

                },
                "已成功修改.",
                () =>
                {
                    LoadDatas();
                });
        }

        private void send()
        {
            if (SelInsp.Status != "作废")
            {
                MsgBox.Error("只有作废的单据可以寄回");
                return;
            }

            UIOperManager.SaveData(
                () =>
                {
                    Frm_ReturnInSurancePolicy returnForm = new Frm_ReturnInSurancePolicy(SelInsp);
                    returnForm.ShowDialog();
                    if (returnForm.NewInsurancePolicy != null)
                    {
                        SelInsp.Status = "已寄回";
                        //this.bds_insurancePolicy.Add(returnForm.NewInsurancePolicy);
                        return true;
                    }
                    else
                        return false;

                    //SelInsp.Status = "已寄回";
                    //return _masterProxy.UpdateInsurancePolicy(SelInsp).IsSuccess;
                },
                "已成功寄回.",
                () =>
                {
                    LoadDatas();
                });
        }

        private void reuse()
        {
            bool isSure = false;
            MsgBox.Question("确定要重用?", () => { isSure = true; }, null);
            if (!isSure)
                return;

            UIOperManager.SaveData(
                () =>
                {
                    if (SelInsp.Status != "作废") 
                    {
                        MsgBox.Error("只有作废的单据可以重用");
                        return false;
                    }

                    SelInsp.Status = "可用";
                    return _masterProxy.UpdateInsurancePolicy(SelInsp).IsSuccess;

                },
                "已成功重用.",
                () =>
                {
                    LoadDatas();
                });
        }


        // selected insurance policy
        public Contract.InsurancePolicy SelInsp
        {
            get;
            set;
        }
    }
}
